package initialize

import (
	"github.com/gin-gonic/gin"
	"mcp-demo/internal/global"
	"mcp-demo/internal/middleware"
	"net/http"
)

// Routers 初始化总路由
func Routers() *gin.Engine {
	Router := gin.New()
	//Router.Use(gin.Recovery())
	//if gin.Mode() == gin.DebugMode {
	//	Router.Use(gin.Logger())
	//}
	Router.Use(middleware.GinRecovery(true), middleware.FileLogger())

	// 运行mcp
	sseServer := McpRun()

	// 方便统一添加路由组前缀
	//公共路由
	PublicGroup := Router.Group(global.CONFIG.System.RouterPrefix)
	// 私有路由
	//PrivateGroup := Router.Group(global.CONFIG.System.RouterPrefix)

	// 健康监测
	PublicGroup.GET("/health", func(c *gin.Context) {
		c.JSON(http.StatusOK, "ok")
	})

	// 注册mcp服务
	Router.POST(global.CONFIG.MCP.MessagePath, func(c *gin.Context) {
		sseServer.MessageHandler().ServeHTTP(c.Writer, c.Request)
	})
	PublicGroup.GET(global.CONFIG.MCP.SSEPath, func(c *gin.Context) {
		sseServer.SSEHandler().ServeHTTP(c.Writer, c.Request)
	})

	global.ROUTERS = Router.Routes()
	global.L.Info("路由注册完成")

	return Router
}
